package com.sean.animation;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.sean.activity.R;
import com.sean.iphoneAppWorkspace.DataVo;
import com.sean.util.UnitUtil;

/**
 * 可拖放的控件
 * @author Sean
 *
 */
public class DragItemView extends ImageView {
	
	public DragItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		this.setBackgroundResource(0);
		mData = new DataVo();
		mData.setIcon(R.drawable.appicon);
		mData.setText("text");
		mText = mData.getText();
		setIcon(mData.getIcon());
		
		mSpace = (int)UnitUtil.dip2px(context, 10);
		
		setShake(true);
	}
	
	public DragItemView(Context context, DataVo data) {
		super(context);
		this.setBackgroundResource(0);
		mData = data;
		mText = mData.getText();
		setIcon(mData.getIcon());
		
		mSpace = (int)UnitUtil.dip2px(context, 10);
	}

	public static final String TAG = "DragItemView";
	
	private static Random RANDOM = new Random();
	
	private static int[] SHAKE_DEGREES = new int[] {-5, -4, -3, -2, -1, 0,  1, 2, 3, 4, 5};
	private boolean mIsShake;
	private boolean mShakeDirection;
	private int mShakeIndex;
	
	private String mText;
	private Drawable mIcon;

	private DataVo mData;
	
	private int mSpace;
	
	private Paint mPaint;
	private int[] mTextPoint;
	
	private Animation mAnimation;
	
	
	
	public DataVo getData() {
		return mData;
	}
	
	public void setIcon(int icon) {
		mIcon = this.getResources().getDrawable(icon);
		this.setImageDrawable(mIcon);
	}
	
	public void setText(String text) {
		this.mText = text;
	}

	/**
	 * 设置图标是否摇晃
	 * @param isShake
	 */
	public void setShake(boolean isShake) {
		mIsShake = isShake;
		mShakeIndex = RANDOM.nextInt(11);
	}
	
	public void startAnimation(float x, float y) {
    	mAnimation = new TranslateAnimation(0, x, 0, y);
    	mAnimation.setDuration(500);
//    	mAnimation.setInterpolator(new CycleInterpolator(1f));
    	mAnimation.setFillAfter(true);
    	this.startAnimation(mAnimation);
    }
	
	public void move() {
		
	}


	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		
		int iconWidth = mIcon.getIntrinsicWidth();
		int iconHeight = mIcon.getIntrinsicHeight();
		int width = this.getMeasuredWidth();
		int height = this.getMeasuredHeight();
		Rect rect = mIcon.getBounds();
		rect.left = width / 2 - iconWidth / 2;
		rect.right = rect.left + iconWidth;
		rect.top = mSpace;
		rect.bottom = rect.top + iconHeight;
		
		mPaint = new Paint();
		mPaint.setTextSize(20);
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.WHITE);
		mTextPoint = new int[2];
		int length = (int)mPaint.measureText(mText);
		mTextPoint[0] = width / 2 - length / 2;
		mTextPoint[1] = mSpace * 2 + iconHeight;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		//画图标
		canvas.save();
		if(mIsShake) {
			if(mShakeIndex >= SHAKE_DEGREES.length - 1) {
				mShakeDirection = true;
			} else if(mShakeIndex <= 0) {
				mShakeDirection = false;
			}
			canvas.rotate(SHAKE_DEGREES[mShakeIndex], this.getMeasuredWidth() / 2, this.getMeasuredHeight() / 2);
			if(mShakeDirection) {
				mShakeIndex --;
			} else {
				mShakeIndex ++;
			}
		}
		mIcon.draw(canvas);
		canvas.restore();
		
		//画标题
		canvas.drawText(mText, mTextPoint[0], mTextPoint[1], mPaint);
	}
	
}
